from fastapi import FastAPI, APIRouter
from starlette.middleware.cors import CORSMiddleware
import uvicorn
import subprocess

app = FastAPI()
origins = ['*']

router = APIRouter()

def call_shell(cmd):
    result = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
    for line in result.stdout.readlines():
        print(line)

@router.get("/bms_proto_webhook")
def bms_proto_webhook(payload):
    call_shell("./pub_bms.sh")   
    return True


if __name__ == '__main__':
    app.include_router(router)
    app.add_middleware(
        CORSMiddleware,
        allow_origins=origins,
        allow_credentials=True,
        allow_methods=["*"],
        allow_headers=["*"],
    )
    uvicorn.run(app, host="0.0.0.0", port=6666, log_level="info", forwarded_allow_ips="*") 